package view.code
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import define.SlotConst;
	
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import mvc.command.client.IArmoryTipClient;
	import mvc.control.ArmoryEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.effects.Glow;
	import mx.events.FlexEvent;
	import mx.events.ToolTipEvent;
	
	import orm.fantasy.RCharItemVO;
	import orm.fantasy.TblCharacterVO;
	import orm.fantasy.TblItemPropertyVO;
	import orm.fantasy.TblItemVO;
	
	import view.design.DesignSlot;

	public class CodeSlot extends Canvas implements IArmoryTipClient
	{
		public var imgSlot:Image;
		public var imgArmory:Image;
		public var effectMouseOver:Glow;

		private var _slotIcon:String;
		private var _tblItemVO:TblItemVO;
		private var _rCharItemVO:RCharItemVO;
		private var _tblItemPropVO:TblItemPropertyVO;
		private var _tblCharacterVO:TblCharacterVO;

		public function CodeSlot()
		{
			super();
		}

		protected function design_initializeHandler(event:FlexEvent):void
		{
		}

		public function set tblCharacterVO(data:TblCharacterVO):void
		{
			_tblCharacterVO = data;
		}

		public function get tblCharacterVO():TblCharacterVO
		{
			return _tblCharacterVO;
		}

		public function get tblItemVO():TblItemVO
		{
			return _tblItemVO;
		}

		public function get rCharItemVO():RCharItemVO
		{
			return _rCharItemVO;
		}

		public function initilaizeArmory(rCharItemVO:RCharItemVO, tblItemVO:TblItemVO, tblItemPropertyVO:TblItemPropertyVO):void
		{
			_rCharItemVO = rCharItemVO;
			_tblItemVO = tblItemVO;
			_tblItemPropVO = tblItemPropertyVO;

			this.toolTip = _tblItemPropVO.info_item_name;
			this.imgArmory.load(File.applicationDirectory.resolvePath(
				"app:/icon/items/" + _tblItemPropVO.info_icon + ".jpg").nativePath);
		}

		public function set slotIcon(data:String):void
		{
			_slotIcon = data;
			
			this.imgSlot.load(File.applicationDirectory.resolvePath(
				"app:/icon/slots/" + _slotIcon + ".jpg").nativePath);
		}

		public function get availableSlots():Array
		{
			return SlotConst.getAvailableSlots(_slotIcon);
		}

		protected function design_mouseOverHandler(event:MouseEvent):void
		{
			// 光圈效果
			if(this.effectMouseOver.isPlaying)
				this.effectMouseOver.stop();
			this.effectMouseOver.play([this], false);
		}

		protected function design_mouseOutHandler(event:MouseEvent):void
		{
			// 光圈效果
			if(this.effectMouseOver.isPlaying)
				this.effectMouseOver.stop();
			this.effectMouseOver.play([this], true);
		}

		protected function design_clickHandler(event:MouseEvent):void
		{
			var mvcEvent:ArmoryEvent = new ArmoryEvent(ArmoryEvent.SEARCH_OPTIONAL_ITEM, null, this as DesignSlot);
			CairngormEventDispatcher.getInstance().dispatchEvent(mvcEvent);
		}

		// IArmoryTipClient
		public function get tipTblItemVO():TblItemVO
		{
			return _tblItemVO;
		}

		protected function design_toolTipCreateHandler(event:ToolTipEvent):void
		{
			var mvcEvent:ArmoryEvent = new ArmoryEvent(ArmoryEvent.SHOW_ARMORY_TIP);
			mvcEvent.data = {client: this, toolTipEvent: event};
			CairngormEventDispatcher.getInstance().dispatchEvent(mvcEvent);
		}
	}
}